<template>
  <div class="role-details-page">
    <div class="container">
      <div class="page-header">
        <button class="back-btn" @click="goBack">
          <span class="back-icon">←</span>
          返回
        </button>
        <h1 class="page-title">角色详情</h1>
      </div>
      
      <div v-if="loading" class="loading-container">
        <div class="loading-spinner"></div>
        <p>加载中...</p>
      </div>
      
      <div v-else-if="error" class="error-container">
        <div class="error-icon">❌</div>
        <h3>加载失败</h3>
        <p>{{ error }}</p>
        <button class="retry-btn" @click="loadRoleDetails">重试</button>
      </div>
      
      <div v-else-if="role" class="role-details">
        <!-- 角色基本信息 -->
        <div class="role-header">
          <div class="role-avatar">
            <img v-if="role.avatarUrl" :src="role.avatarUrl" :alt="role.name" />
            <div v-else class="avatar-placeholder">{{ role.name.charAt(0) }}</div>
            <div class="role-status" :class="role.status.toLowerCase()">
              {{ getStatusText(role.status) }}
            </div>
          </div>
          
          <div class="role-info">
            <h2 class="role-name">{{ role.name }}</h2>
            <p class="role-desc">{{ role.description || '暂无描述' }}</p>
            
            <div class="role-stats">
              <div class="stat-item">
                <span class="stat-icon">💬</span>
                <span class="stat-label">使用次数</span>
                <span class="stat-value">{{ role.usageCount || 0 }}</span>
              </div>
              <div class="stat-item">
                <span class="stat-icon">⭐</span>
                <span class="stat-label">平均评分</span>
                <span class="stat-value">{{ role.averageRating || 0 }}</span>
              </div>
              <div class="stat-item">
                <span class="stat-icon">📅</span>
                <span class="stat-label">创建时间</span>
                <span class="stat-value">{{ formatDate(role.createdAt) }}</span>
              </div>
            </div>
          </div>
          
          <div class="role-actions">
            <button class="action-btn primary" @click="startChat">开始对话</button>
            <button class="action-btn" @click="editRole">编辑角色</button>
          </div>
        </div>
        
        <!-- 角色详细信息 -->
        <div class="role-content">
          <div class="content-section">
            <h3 class="section-title">性格特点</h3>
            <div class="section-content">
              <p v-if="role.personality">{{ role.personality }}</p>
              <p v-else class="empty-text">暂无性格描述</p>
            </div>
          </div>
          
          <div class="content-section">
            <h3 class="section-title">语言风格</h3>
            <div class="section-content">
              <p v-if="role.languageStyle">{{ role.languageStyle }}</p>
              <p v-else class="empty-text">暂无语言风格描述</p>
            </div>
          </div>
          
          <div class="content-section">
            <h3 class="section-title">背景故事</h3>
            <div class="section-content">
              <p v-if="role.background">{{ role.background }}</p>
              <p v-else class="empty-text">暂无背景故事</p>
            </div>
          </div>
          
          <div class="content-section">
            <h3 class="section-title">提示词模板</h3>
            <div class="section-content">
              <div v-if="role.promptTemplate" class="code-block">
                <pre>{{ role.promptTemplate }}</pre>
              </div>
              <p v-else class="empty-text">暂无提示词模板</p>
            </div>
          </div>
          
          <div class="content-section">
            <h3 class="section-title">开场白</h3>
            <div class="section-content">
              <p v-if="role.openingLine">{{ role.openingLine }}</p>
              <p v-else class="empty-text">暂无开场白</p>
            </div>
          </div>
          
          <div class="content-section">
            <h3 class="section-title">标签</h3>
            <div class="section-content">
              <div v-if="role.tags" class="tags-container">
                <span v-for="tag in role.tags.split(',')" :key="tag" class="tag">
                  {{ tag.trim() }}
                </span>
              </div>
              <p v-else class="empty-text">暂无标签</p>
            </div>
          </div>
          
          <div class="content-section">
            <h3 class="section-title">默认音色</h3>
            <div class="section-content">
              <p>{{ role.defaultVoiceType || 'qiniu_zh_female_wwxkjx' }}</p>
            </div>
          </div>
          
          <div class="content-section">
            <h3 class="section-title">隐私设置</h3>
            <div class="section-content">
              <span class="privacy-badge" :class="role.privacy.toLowerCase()">
                {{ role.privacy === 'PUBLIC' ? '公开' : '私有' }}
              </span>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'

const route = useRoute()
const router = useRouter()
const loading = ref(false)
const error = ref('')
const role = ref(null)

const loadRoleDetails = async () => {
  loading.value = true
  error.value = ''
  
  try {
    const token = localStorage.getItem('auth_token')
    if (!token) {
      router.push('/login')
      return
    }
    
    const res = await fetch(`/api/roles/${route.params.id}`, {
      headers: { 'Authorization': `Bearer ${token}` }
    })
    
    const data = await res.json()
    if (data.code === 0) {
      role.value = data.data
    } else {
      error.value = data.message || '获取角色详情失败'
    }
  } catch (e) {
    console.error('加载角色详情失败', e)
    error.value = '网络错误，请重试'
  } finally {
    loading.value = false
  }
}

const getStatusText = (status) => {
  const statusMap = {
    'PUBLISHED': '已发布',
    'DRAFT': '草稿',
    'PENDING': '待审核',
    'REJECTED': '已拒绝'
  }
  return statusMap[status] || status
}

const formatDate = (dateStr) => {
  if (!dateStr) return ''
  const date = new Date(dateStr)
  return date.toLocaleDateString('zh-CN', {
    year: 'numeric',
    month: 'long',
    day: 'numeric',
    hour: '2-digit',
    minute: '2-digit'
  })
}

const goBack = () => {
  router.back()
}

const startChat = () => {
  router.push(`/chat/${role.value.id}`)
}

const editRole = () => {
  router.push(`/edit-role/${role.value.id}`)
}

onMounted(() => {
  loadRoleDetails()
})
</script>

<style scoped>
.role-details-page {
  min-height: 100vh;
  background: #f8fafc;
  padding: 20px 0;
}

.container {
  max-width: 1000px;
  margin: 0 auto;
  padding: 0 20px;
}

.page-header {
  display: flex;
  align-items: center;
  gap: 20px;
  margin-bottom: 30px;
}

.back-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 16px;
  background: white;
  border: 1px solid #e5e7eb;
  border-radius: 8px;
  color: #374151;
  text-decoration: none;
  font-size: 14px;
  cursor: pointer;
  transition: all 0.2s;
}

.back-btn:hover {
  background: #f9fafb;
  border-color: #d1d5db;
}

.back-icon {
  font-size: 16px;
}

.page-title {
  font-size: 2rem;
  font-weight: 700;
  color: #1f2937;
  margin: 0;
}

.loading-container {
  text-align: center;
  padding: 60px 20px;
  color: #6b7280;
}

.loading-spinner {
  width: 40px;
  height: 40px;
  border: 4px solid #e5e7eb;
  border-top: 4px solid #667eea;
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin: 0 auto 16px;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

.error-container {
  text-align: center;
  padding: 60px 20px;
  color: #6b7280;
}

.error-icon {
  font-size: 3rem;
  margin-bottom: 16px;
}

.error-container h3 {
  font-size: 1.5rem;
  font-weight: 600;
  color: #374151;
  margin-bottom: 8px;
}

.retry-btn {
  margin-top: 16px;
  padding: 8px 16px;
  background: #667eea;
  color: white;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  transition: background 0.2s;
}

.retry-btn:hover {
  background: #5a67d8;
}

.role-details {
  background: white;
  border-radius: 16px;
  box-shadow: 0 4px 6px rgba(0,0,0,0.05);
  overflow: hidden;
}

.role-header {
  padding: 30px;
  border-bottom: 1px solid #e5e7eb;
  display: flex;
  gap: 24px;
  align-items: flex-start;
}

.role-avatar {
  position: relative;
  width: 120px;
  height: 120px;
  border-radius: 50%;
  overflow: hidden;
  background: linear-gradient(135deg, #667eea, #764ba2);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.role-avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.avatar-placeholder {
  color: white;
  font-size: 48px;
  font-weight: 600;
}

.role-status {
  position: absolute;
  top: -4px;
  right: -4px;
  padding: 4px 8px;
  border-radius: 12px;
  font-size: 12px;
  font-weight: 500;
  color: white;
}

.role-status.published {
  background: #10b981;
}

.role-status.draft {
  background: #f59e0b;
}

.role-status.pending {
  background: #3b82f6;
}

.role-info {
  flex: 1;
}

.role-name {
  font-size: 2rem;
  font-weight: 700;
  color: #1f2937;
  margin: 0 0 12px 0;
}

.role-desc {
  color: #6b7280;
  line-height: 1.6;
  margin-bottom: 20px;
  font-size: 1.1rem;
}

.role-stats {
  display: flex;
  gap: 24px;
  flex-wrap: wrap;
}

.stat-item {
  display: flex;
  align-items: center;
  gap: 8px;
  color: #6b7280;
}

.stat-icon {
  font-size: 18px;
}

.stat-label {
  font-size: 14px;
}

.stat-value {
  font-weight: 600;
  color: #374151;
}

.role-actions {
  display: flex;
  gap: 12px;
  flex-shrink: 0;
}

.action-btn {
  padding: 12px 24px;
  border: none;
  border-radius: 8px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.action-btn.primary {
  background: linear-gradient(135deg, #667eea, #764ba2);
  color: white;
}

.action-btn.primary:hover {
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(102, 126, 234, 0.3);
}

.action-btn:not(.primary) {
  background: #f3f4f6;
  color: #374151;
  border: 1px solid #d1d5db;
}

.action-btn:not(.primary):hover {
  background: #e5e7eb;
}

.role-content {
  padding: 30px;
}

.content-section {
  margin-bottom: 32px;
}

.content-section:last-child {
  margin-bottom: 0;
}

.section-title {
  font-size: 1.25rem;
  font-weight: 600;
  color: #1f2937;
  margin-bottom: 12px;
  padding-bottom: 8px;
  border-bottom: 2px solid #e5e7eb;
}

.section-content {
  color: #4b5563;
  line-height: 1.6;
}

.empty-text {
  color: #9ca3af;
  font-style: italic;
}

.code-block {
  background: #f8fafc;
  border: 1px solid #e5e7eb;
  border-radius: 8px;
  padding: 16px;
  overflow-x: auto;
}

.code-block pre {
  margin: 0;
  font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;
  font-size: 14px;
  line-height: 1.5;
  white-space: pre-wrap;
  word-break: break-word;
}

.tags-container {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.tag {
  background: #e0e7ff;
  color: #3730a3;
  padding: 4px 12px;
  border-radius: 16px;
  font-size: 14px;
  font-weight: 500;
}

.privacy-badge {
  padding: 6px 12px;
  border-radius: 6px;
  font-size: 14px;
  font-weight: 500;
}

.privacy-badge.public {
  background: #dcfce7;
  color: #166534;
}

.privacy-badge.private {
  background: #fef3c7;
  color: #92400e;
}

@media (max-width: 768px) {
  .role-header {
    flex-direction: column;
    text-align: center;
  }
  
  .role-actions {
    justify-content: center;
  }
  
  .role-stats {
    justify-content: center;
  }
  
  .container {
    padding: 0 16px;
  }
  
  .role-header,
  .role-content {
    padding: 20px;
  }
}
</style>

